<!DOCTYPE html>
<html>
	<head>
		<link rel="stylesheet" href="index.css" type="text/css" media="screen" />
	
		<script src="jquery-1.6.min.js" type="text/javascript"></script>
		<script src="../public/jquery.syntax.js" type="text/javascript"></script>
		<script src="../public/jquery.syntax.cache.js" type="text/javascript"></script>
		
		<script type="text/javascript">
			$(function() {
				jQuery.syntax();
			});
		</script>
 
	</head>
	<body>
		<h1>Syntax: C, C++, Objective-C</h1>
		
		<h2>C++</h2>
		
		<pre class="syntax brush-clang">#include &lt;iostream&gt;

// If we increase row by 1, the offset will increase by sz (number of elements per row i.e. number of columns)
// If we increase col by 1, the offset will increase by 1
unsigned rowMajorOffset(unsigned row, unsigned col, unsigned sz)
{
    return col + row * sz;
}

// If we increase col by 1, the offset will increase by sz (number of elements per column i.e. number of rows)
// If we increase row by 1, the offset will increase by 1
unsigned columnMajorOffset(unsigned row, unsigned col, unsigned sz)
{
    return row + col * sz;
}

template &lt;typename _ValueT, unsigned _R, unsigned _C, bool _ColumnMajor&gt;
class Matrix {
protected:
	enum { ColumnMajor = _ColumnMajor };
	enum { R = _R };
	enum { C = _C };

	typedef _ValueT ValueT;

	ValueT m_values[C*R];

public:
	const ValueT &amp; at (unsigned r, unsigned c) const
	{
		if (ColumnMajor)
			return m_values[columnMajorOffset(r, c, R)];
		else
			return m_values[rowMajorOffset(r, c, C)];
	}

	ValueT &amp; at (unsigned r, unsigned c)
	{
		if (ColumnMajor)
			return m_values[columnMajorOffset(r, c, R)];
		else
			return m_values[rowMajorOffset(r, c, C)];
	}

	void loadTestPattern ()
	{
		for (unsigned r = 0; r &lt; R; r += 1)
			for (unsigned c = 0; c &lt; C; c += 1)
				at(r, c) = (r+1) * 1000 + (c+1);
	}

	void debug ()
	{
		using namespace std;

		if (ColumnMajor)
			cout &lt;&lt; &quot;Column-Major Matrix &quot; &lt;&lt; &quot;(&quot; &lt;&lt; R &lt;&lt; &quot;,&quot; &lt;&lt; C &lt;&lt; &quot;)&quot; &lt;&lt; &quot; @ &quot; &lt;&lt; this &lt;&lt; endl;
		else
			cout &lt;&lt; &quot;Row-Major Matrix &quot; &lt;&lt; &quot;(&quot; &lt;&lt; R &lt;&lt; &quot;,&quot; &lt;&lt; C &lt;&lt; &quot;)&quot; &lt;&lt; &quot; @ &quot; &lt;&lt; this &lt;&lt; endl;

		cout &lt;&lt; &quot;Memory Offset: &quot;;
		for (unsigned i = 0; i &lt; (R*C); i += 1)
			cout &lt;&lt; i &lt;&lt; &quot;    &quot;;
		cout &lt;&lt; endl;

		cout &lt;&lt; &quot;       Values: &quot;;	
		for (unsigned i = 0; i &lt; (R*C); i += 1)
			cout &lt;&lt; m_values[i] &lt;&lt; &quot; &quot;;
		cout &lt;&lt; endl;

		cout &lt;&lt; &quot;Standard Mathematical Notation:&quot; &lt;&lt; endl;
		cout &lt;&lt; &quot;      &quot;;
		for (unsigned c = 0; c &lt; C; c += 1)
			cout &lt;&lt; &quot;Col &quot; &lt;&lt; c &lt;&lt; &quot; &quot;;
		cout &lt;&lt; endl;

		for (unsigned r = 0; r &lt; R; r += 1) {
			cout &lt;&lt; &quot;Row &quot; &lt;&lt; r &lt;&lt; &quot; &quot;;
			for (unsigned c = 0; c &lt; C; c += 1)
				cout &lt;&lt; at(r, c) &lt;&lt; &quot;  &quot;;
			cout &lt;&lt; endl;
		}
		cout &lt;&lt; endl;
	}

	Matrix&lt;ValueT, R, C, !ColumnMajor&gt; transposeStorage () const
	{
		Matrix&lt;ValueT, R, C, !ColumnMajor&gt; result;

		for (unsigned r = 0; r &lt; R; r += 1)
			for (unsigned c = 0; c &lt; C; c += 1)
				result.at(r, c) = at(r, c);

		return result;
	}

	Matrix&lt;ValueT, C, R, !ColumnMajor&gt; transposeMatrix () const
	{
		Matrix&lt;ValueT, C, R, !ColumnMajor&gt; result;

		memcpy(&amp;result.at(0,0), m_values, sizeof(m_values));

		return result;
	}
};

int main (int argc, char * const argv[]) {
	Matrix&lt;float, 4, 2, false&gt; rowMajorMatrix;
	Matrix&lt;float, 4, 2, true&gt; columnMajorMatrix;

	rowMajorMatrix.loadTestPattern();
	rowMajorMatrix.debug();

	columnMajorMatrix.loadTestPattern();
	columnMajorMatrix.debug();

	rowMajorMatrix = columnMajorMatrix.transposeStorage();
	rowMajorMatrix.debug();

	Matrix&lt;float, 2, 4, false&gt; transposedMatrix = columnMajorMatrix.transposeMatrix();
	transposedMatrix.debug();

	return 0;
}</pre>

	<h2>Objective-C Header</h2>

	<pre class="syntax brush-clang">//
//  GHInventoryEditor.h
//  Goblin Hacker
//
//  Created by Samuel Williams on 19/12/07.
//  Copyright 2007 Samuel Williams, Orion Transfer Ltd. All rights reserved.

//  This software was originally produced by Orion Transfer Ltd.
//    Please see http://www.oriontransfer.org for more details.
//

/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.
*/


#import &lt;Cocoa/Cocoa.h&gt;

#import &quot;GHEditorController.h&quot;
#import &quot;GHSavedGameDocument+Slots.h&quot;

@interface GHInventoryEditor : GHEditorController {
	NSInteger armorType, weaponType, potionQuality;
}

+ (NSArray*) items;

@property NSInteger armorType;
@property NSInteger weaponType;
@property NSInteger potionQuality;

- (IBAction)healingPotions: (id)sender;
- (IBAction)manaPotions: (id)sender;
- (IBAction)cureDiseasePotions: (id)sender;
- (IBAction)catsEyePotions: (id)sender;
- (IBAction)predatorSightPotions:(id)sender;
- (IBAction)leatherSkinPotions: (id)sender;
- (IBAction)hastePotions: (id)sender;
- (IBAction)invisibilityPotions: (id)sender;

- (IBAction)basicWeapon: (id)sender;
- (IBAction)goodWeapon: (id)sender;
- (IBAction)superbWeapon: (id)sender;
- (IBAction)avatarWeapon: (id)sender;

- (IBAction)basicArmor: (id)sender;
- (IBAction)goodArmor: (id)sender;
- (IBAction)superbArmor: (id)sender;
- (IBAction)avatarArmor: (id)sender;

- (IBAction)alchemyBooks: (id)sender;
- (IBAction)amulets: (id)sender;
- (IBAction)lockpicks: (id)sender;
- (IBAction)reagents: (id)sender;
- (IBAction)reactants: (id)sender;
- (IBAction)rings: (id)sender;
- (IBAction)torches: (id)sender;

- (IBAction)avatarJewelry: (id)sender;

@end</pre>

	<h2>Objective-C Implementation</h2>
	
	<pre class="syntax brush-clang">//
//  GPointSet.m
//  Gocoa
//
//  Created by Samuel Williams on 9/05/05.
//  Copyright 2005 Orion Transfer Ltd. All rights reserved.
//

#import &quot;GPointSet.h&quot;

void adjacentPoints (NSPoint p, NSPoint pts[4]) { //pts must have four elements
	pts[3] = NSMakePoint (p.x - 1, p.y);
	pts[0] = NSMakePoint (p.x, p.y + 1);
	pts[1] = NSMakePoint (p.x + 1, p.y);
	pts[2] = NSMakePoint (p.x, p.y - 1);
}

@implementation GPointSet
+ (NSPoint) pointForIndex:(unsigned int)index withSize:(NSSize)s {
	int x = index % (int)(s.width);
	return NSMakePoint (x, (int)((index - x) / s.width));
}

+ (unsigned int) indexForPoint:(NSPoint)point withSize:(NSSize)s {
	return point.x + (point.y * s.width);
}

- (id) init {
	NSLog(@&quot;%s does not have default constructor&quot;, __FUNCTION__);
	return nil;
}

- (id) initWithSize:(NSSize)newSize {
	if (self = [super init]) {
		size = newSize;
		indexSet = [[NSMutableIndexSet alloc] init];
	}

	return self;
}

- (void) dealloc {
	[super dealloc];
	[indexSet release];
}

- (NSMutableIndexSet*) indexSet {
	return [[indexSet retain] autorelease];
}

- (void) addPoint:(NSPoint)point {
	[indexSet addIndex:[GPointSet indexForPoint:point withSize:size]];
}

- (void) removePoint:(NSPoint)point {
	[indexSet removeIndex:[GPointSet indexForPoint:point withSize:size]];
}

- (BOOL) containsPoint:(NSPoint)point {
	return [indexSet containsIndex:[GPointSet indexForPoint:point withSize:size]];
}

- (NSPoint) pointForIndex:(unsigned int)index {
	return [GPointSet pointForIndex:index withSize:size];
}

- (unsigned int) indexForPoint:(NSPoint)point {
	return [GPointSet indexForPoint:point withSize:size];
}

- (NSString*)description {
	NSMutableString *result = [NSMutableString new];
	[result appendFormat:@&quot;&lt;GPointSet %h&quot;, self];

	unsigned index = [indexSet firstIndex];

	while (index != NSNotFound) {
		[result appendFormat:@&quot;%@ &quot;, NSStringFromPoint([self pointForIndex:index])];

		index = [indexSet indexGreaterThanIndex:index];
	}

	[result appendString:@&quot;&gt;&quot;];
	return result;
}

@end</pre>

	<h2>Strings with embedded escapes</h2>
	
	<pre class="syntax brush-clang">
const char * text = "The elephant\n was in the room.";
	</pre>

	<h2>Types and Functions</h2>

	<pre class="syntax c++">
int   pthread_setcancelstate(int, int *);
int   pthread_setcanceltype(int, int *);
int   pthread_setconcurrency(int);
int   pthread_setschedparam(pthread_t, int ,
      const struct sched_param *);
int   pthread_setspecific(pthread_key_t, const void *);
void  pthread_testcancel(void);

namespace Bob {
	namespace moo {
		
	}
}
	</pre>
	
	</body>
</html>